package com.hbt.android.kotlin.http

import android.util.Log
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Query
import java.util.concurrent.TimeUnit


fun main() {
    val apiService = RetrofitTest.create(ApiService::class.java)
    apiService.queryUser("1302425").enqueue(object :Callback<Movie>{
        override fun onResponse(call: Call<Movie>, response: Response<Movie>) {
            Log.e("Retrofit", response.body()?.toString() ?: "response is null")
        }

        override fun onFailure(call: Call<Movie>, t: Throwable) {
            Log.e("Retrofit", t.localizedMessage ?: "unknown reason")
        }

    })
}

//public const val BASE_GET_URL = "https://movie.querydata.org/api?id=1302425"
//public const val BASE_GET_URL = "https://movie.querydata.org"
public const val BASE_GET_URL = "https://www.songyubao.com/"

object RetrofitTest {

    private val client: OkHttpClient
    init {
        val httpLoggingInterceptor = HttpLoggingInterceptor()
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY)

        client = OkHttpClient.Builder() // builder 构造者设计模式
            .connectTimeout(10, TimeUnit.SECONDS)   // 连接超时时间
            .readTimeout(10, TimeUnit.SECONDS)  // 读取超时
            .writeTimeout(10, TimeUnit.SECONDS) // 请求超时，
//            .addInterceptor(HttpLoggingInterceptor())// 内置的日志拦截器
//            .addInterceptor(LoggingInterceptor()) // 自定义的拦截器
            .addInterceptor(httpLoggingInterceptor) // 自定义的拦截器
            .build()
    }
    private var retrofit: Retrofit = Retrofit.Builder()
        .client(client)
        .baseUrl(BASE_GET_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    fun <T> create(clazz: Class<T>): T {
        return retrofit.create(clazz)
    }
}

interface ApiService {
    @GET(value = "/api")
    fun queryUser(@Query(value = "id", encoded = true) id:String):Call<Movie>

    @GET(value = "static/book/assets/study.json")
    fun getStudy(): Call<List<Course>>
}


data class Course(
    var label: String,
    var poster: String,
    var progress: String,
    var title: String
)

data class Movie(
    val actor: List<Actor>,
    val alias: String,
    val `data`: List<DataX>,
    val dateReleased: String,
    val director: List<Director>,
    val doubanId: String,
    val doubanRating: String,
    val doubanVotes: Int,
    val duration: Int,
    val episodes: Any,
    val id: String,
    val imdbId: String,
    val imdbRating: String,
    val imdbVotes: Int,
    val originalName: String,
    val rottenRating: String,
    val rottenVotes: Int,
    val totalSeasons: Any,
    val type: String,
    val writer: List<Writer>,
    val year: String
)

data class Actor(
    val `data`: List<Data>
)

data class DataX(
    val country: String,
    val description: String,
    val genre: String,
    val lang: String,
    val language: String,
    val name: String,
    val poster: String
)

data class Director(
    val `data`: List<DataXX>
)

data class Writer(
    val `data`: List<DataXXX>
)

data class Data(
    val lang: String,
    val name: String
)

data class DataXX(
    val lang: String,
    val name: String
)

data class DataXXX(
    val lang: String,
    val name: String
)